//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Chart.Serialization;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class AttachedChart: IOfficeXmlSerializable
    {
        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(ChartConstants.ChartNamespace + "chart"))
            {
                string id = c.GetString(OfficeConstants.RelationshipNamespace + "id");
                using (var relatedContext = c.OpenRelatedContext(id, ChartConstants.ChartNamespaceUri))
                {
                    ChartArea = new ChartArea();
                    IOfficeXmlSerializable o = ChartArea;
                    o.ReadXml(relatedContext);
                }
            }

        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(ChartConstants.ChartNamespace + "chart"))
            {
                using (var relatedContext = c.CreateRelatedContext(ChartConstants.ChartNamespace,
                    c.GetAvailableUri("../charts/chart{0}.xml"), ChartConstants.ChartRelationshipType, ChartConstants.ChartContentType))
                {
                    relatedContext.WriteElement(ChartArea);
                    c.SetString(OfficeConstants.RelationshipNamespace + "id", relatedContext.Relationship.Id);
                }
            }
        }

        public ChartArea ChartArea { get; set; }
    }
}